"""
this module is to get user information from the request
the cookie key
doubanaccesstoken
sinaaccesstoken
"""
from google.appengine.ext import db
from weibo.models import Sina_Access_Token
from doubancon.models import Douban_Access_Token
from myuser.models import MyUser

def getUser(request):
    """
    the method for user to be in login status
    """
    #1 this site reg
    if request.COOKIES.has_key("key") and request.COOKIES.has_key("pwd"):
        thekey = request.COOKIES["key"]
        pwd = request.COOKIES["pwd"]
        theuser = db.get(thekey)
        if theuser.pwd == pwd:
            return theuser
        else:
            return None


#weibo and douban should supply some api
#for examples:
#1get the user info according to the accesstoken
#2check the access token valid

def checkSinaAccessToken(request):
    #if ok return infomation about this user
    if request.COOKIES.has_key('sinaaccesstoken') and request.COOKIES.has_key("suid"):
        ak = request.COOKIES['sinaaccesstoken']
        suid = request.COOKIES['suid']
        if Sina_Access_Token.all().filter("sinaid = ",suid).filter("thekey = ",ak).count() == 1:
            #return information about this user
            return Sina_Access_Token.all().filter("sinaid = ",suid).filter("thekey = ",ak)[0]
        else:
            #return suid and status is need to relogin with sina
            return None
    else:
        return None

def checkDoubanAccessToken(request):
    #if ok return information about this user
    if request.COOKIES.has_key('doubanaccesstoken'):
        ak = request.COOKIES['doubanaccesstoken']
        duid = request.COOKIES['duid']
        if Douban_Access_Token.all().filter("doubanid =",duid).filter("thekey =",ak).count() == 1:
            return  Douban_Access_Token.all().filter("doubanid =",duid).filter("thekey =",ak)[0]
        else:
            return None
    else:
        return None
    
def checkUserNameValid(name):
    """
    if the username has been registered,the return false
    """
    if MyUser.all().filter("nickname = ",name).count() > 0:
        return False
    return True

def checkEmailValid(email):
    """
    if email has been used,return false
    """
    if MyUser.all().filter("email = ",email).count() > 0:
        return False
    return True

def checkSiteUserValid(username,pwd):
    if MyUser.all().filter("nickname =",username).filter("pwd =",pwd).count() !=1:
        return False
    return True

def isSinaConnect(userkey):
    if Sina_Access_Token.all().filter("userref = ",userkey).count()>0:
        return True
    return False

def isDoubanConnect(userkey):
    if Douban_Access_Token.all().filter("userref = ",userkey).count() > 0:
        return True
    return False


def getSiteUser(username,pwd):
    if checkSiteUserValid(username,pwd) == False:
        return None
    return MyUser.all().filter("nickname =",username).filter("pwd =",pwd)[0]

#this is relized by the hola/oauth
def getCurUser(request):
    """
    get the info of current user
    may login with reg,google,douban,sina
    """


def checkRequestValid(request):
    """
    check whether information in the request are valid
    check douban access token valid
    check sina access token valid
    """
